package org.game.player;

import org.game.obj.GWObject;
import org.game.obj.building.GWCastle;
import org.game.obj.unit.GWPeasant;
import org.game.obj.unit.GWUnit;

public class GWPlayer {
	
	public static final int UNIT_LIMIT = 30;
	private int player;
	
	private int kills;
	
	private int food;
	
	private int wood;
	
	private int steel;
	
	private GWCastle castle;
	
	private GWUnit[] unit;

	public GWPlayer(int pl) {
		player = pl;
		food = 300;
		wood = 300;
		steel = 50;
		kills = 0;
		
		castle = null;
		
		unit = new GWUnit[UNIT_LIMIT];
		for(int i = 0; i < unit.length; i++) {
			unit[i] = null;
		}
	}
	
	public GWPlayer(GWPlayer pl) {
		this.player = pl.player;
		this.food = pl.food;
		this.wood = pl.wood;
		this.steel = pl.steel;
		this.kills = pl.kills;
		
		this.castle = pl.castle;
		
		this.unit = new GWUnit[pl.unit.length];
		for(int i = 0; i < unit.length; i++) {
			if(pl.unit[i] == null) {
				this.unit[i] = null;
			} else {
				this.unit[i] = new GWUnit(pl.unit[i]);
			}
		}
	}
	
	public boolean canAdd(GWObject obj) {
		
		if(obj instanceof GWCastle) {
			if(castle != null) return false;
			castle = (GWCastle) obj;
			return true;
		}
		
		if(obj instanceof GWUnit) {
			GWUnit un = (GWUnit) obj;
			if(castle == null) return false;
			if(firstFreeIndex(unit) < 0) return false;
			if(getFood() - un.getFoodCost() < 0) return false;
			if(getWood() - un.getWoodCost() < 0) return false;
			if(getSteel() - un.getSteelCost() < 0) return false;
			return true;
		}
		return false;
	}

	/**
	 * Use with canAdd!!
	 * @param obj
	 */
	public void add(GWObject obj) {
		
		if(obj instanceof GWCastle) {
			castle = (GWCastle) obj;
			return;
		}
		
		if(obj instanceof GWUnit) {
			int i = firstFreeIndex(unit);
			GWUnit un = (GWUnit) obj;
			un.setNumber(i);
			un.setPlayer(getPlayer());
			unit[i] = un;
			setFood(getFood() - un.getFoodCost());
			setWood(getWood() - un.getWoodCost());
			setSteel(getSteel() - un.getSteelCost());
			return;
		}
	}
	
	private int firstFreeIndex(GWObject[] obj) {
		for(int i = 0; i < obj.length; i++) {
			if(obj[i] == null) return i;
		}
		return -1;
	}
	
	public void addResources(int type) {
		switch(type) {
		case GWPeasant.FOOD:
			food += 10;
			break;
		case GWPeasant.WOOD:
			wood += 10;
			break;
		case GWPeasant.IRON_ORE:
			steel += 10;
			break;
		}
	}
	
	public GWUnit getUnit(int i) {
		return unit[i];
	}
	
	public void setUnit(int i, GWUnit u) {
		this.unit[i] = u;
	}
	
	public int getPlayer() {
		return player;
	}

	public void setPlayer(int player) {
		this.player = player;
	}

	public int getKills() {
		return kills;
	}

	public void setKills(int kills) {
		this.kills = kills;
	}
	
	public GWCastle getCastle() {
		return castle;
	}

	public int getFood() {
		return food;
	}

	public void setFood(int food) {
		this.food = food;
	}

	public int getWood() {
		return wood;
	}

	public void setWood(int wood) {
		this.wood = wood;
	}

	public int getSteel() {
		return steel;
	}

	public void setSteel(int ironOre) {
		this.steel = ironOre;
	}
}
